﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Mmht.DB;

namespace Mmht.BT
{
    public class UploadExcel
    {
        UploadExcelDB ue = new UploadExcelDB();

        public DataTable ExcelToDT(string Path, String SheetName)
        {
            return ue.ExcelToDataTable(Path, SheetName);
        }

        public string UpdateData(string sheetname,DataTable dt,int create_by)
        {
            return ue.UpdateTable(sheetname,dt,create_by);
        }

        /// <summary>
        /// 判断数据条目是否存在，存在则删除再进行插入操作
        /// </summary>
        private string KeyEnable(DataTable dt)
        {
            string message = string.Empty;
            if (dt == null)
            {
                message = "数据信息不存在，请选择其他数据表！";
                return message;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string key = dt.Rows[i][1].ToString();
                if (key.Length == 0)
                {
                    message += "第"+i.ToString()+"行的数据第一列存在空值，请确认数据正确性后再上传！";
                    return message;
                }
            }
            message="数据正常！";
            return message;
        }


        /// <summary>
        /// 判断上传文件是否为Excel文件
        /// </summary>
        /// <param name="filepath">文件路径名</param>
        /// <returns>布尔值</returns>
        public bool CheckExcel(string filepath)
        {
            string filetype = filepath.Substring(filepath.Length - 3, 3);
            if (filetype.ToUpper().CompareTo("XLS") == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得文件中存在的全部工作表名
        /// </summary>
        /// <param name="Path">excel文档路径</param>
        /// <returns>excel文档中工作表明列表</returns>
        public DataTable GetSheetsName(string filepath)
        {
            return ue.GetSheetsName(filepath);
        }

        
    }
}
